package com.dtcnet.skmvp.base;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import com.tencent.mmkv.MMKV;
import com.dtcnet.skmvp.dagger.component.AppComponent;
import com.dtcnet.skmvp.network.RetrofitManager;
import com.dtcnet.skmvp.utils.Log2f;
import com.dtcnet.skmvp.utils.MyToast;

public abstract class BaseApplication extends Application implements IApplication, IAppManager {

    private AppComponent appComponent;
    private static Context appContext;

    public AppComponent getAppComponent() {
        return appComponent;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//        appComponent = DaggerAppComponent.builder().build();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        appContext = getApplicationContext();

        // 是否打印日志
        Log2f.LOG_CAT = isDebug();

        RetrofitManager.config(isDebug());

        MyToast.init(this);

        // 初始化mmkv
        MMKV.initialize(this);

        // 子类调用
        initConfig();

        // 清理日志
        Log2f.cleanLog();

        // 程序启动打印日志
        Log2f.i("-----------程序启动-----------Application Start-----------");
    }

    public void toSelfSetting() {
        Intent mIntent = new Intent();
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            mIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            mIntent.setData(Uri.fromParts("package", getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            mIntent.setAction(Intent.ACTION_VIEW);
            mIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
            mIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
        }
        startActivity(mIntent);
    }

    public static Context getContext() {
        return appContext;
    }

    /**
     * 获取设备序列号
     *
     * @return 设备序列号
     */
    public static String getImei() {
        return Build.SERIAL;
    }
}
